package com.yunfangdata.web.controller;


import com.yunfangdata.domain.IdCardPicture;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IIdCardPictureService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import com.yunfangdata.util.UploadUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("idCardPicture")
public class IdCardPictureController {
    @Autowired
    private IIdCardPictureService idCardPictureService;

    @Autowired
    private ServletContext ctx;


    @RequiresPermissions("idCardPicture:imagePath")
    @PermissionName("身份证图片更新或保存")
    @RequestMapping("imagePath")
    @ResponseBody
    public IdCardPicture imagePath(String staffId){
        IdCardPicture idCardPicture = idCardPictureService.selectIdCardPictureByStaffId(staffId);
        return idCardPicture;
    }
    
    @RequiresPermissions("idCardPicture:saveOrUpdate")
    @PermissionName("身份证图片更新或保存")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(IdCardPicture entity , MultipartFile pic, HttpServletRequest request) {
        JsonResult jsonResult = new JsonResult();
        if(pic != null){
            if(entity.getImagePath() != null && entity.getImagePath().length() != 0) {
                UploadUtil.deleteFile(request.getServletContext(), entity.getImagePath());
            }
            String basePath = ctx.getRealPath("/upload");
            String imagePath = UploadUtil.upload(pic,basePath );
            entity.setImagePath(imagePath);
        }
        if (entity.getId() != null) {
            try {
                idCardPictureService.update(entity);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult = new JsonResult("更新失败");
            }
        } else {
            try {
                idCardPictureService.save(entity);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult = new JsonResult("保存失败");
            }
        }
        return jsonResult;
    }

    @RequiresPermissions("idCardPicture:delete")
    @PermissionName("身份证图片删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        if (id != null) {
            try {
                idCardPictureService.delete(id);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult = new JsonResult("删除失败");
            }
        }
        return jsonResult;
    }
}
